#ifndef __COMPRESS__H__
#define __COMPRESS__H__

#include <zlib.h>
#include <vector>
#include <pthread.h>
#include <Singleton.h>
#include <helper_unit.h>



HTTP_SVR_NS_BEGIN


using namespace std;

class CHelperUnit;

enum GzipThdStatus
{
    GZIP_THD_UNINIT,
    GZIP_THD_RUNNING,
    GZIP_THD_STOPPED
};

enum GzipThdSignal
{
    GZIP_THD_RUN_SIG,
    GZIP_THD_PAUSE_SIG,
    GZIP_THD_STOP_SIG,
    GZIP_THD_RESUME_SIG
};


struct gzip_task
{
public:    
    CHelperUnit*  _gzip_obj;
};


class gzip_thread
{
public:
    gzip_thread();
    ~gzip_thread();
    int Create_thread();
    int Kill_thread();
    static void* Gzip_running(void*);
    inline pthread_t Get_threadId() {return _tid;}
    inline vector<gzip_task>* Get_waiting_task_queue(){return _waiting_task_queue;}
    inline void Set_waiting_task_queue(vector<gzip_task>*& queue) {_waiting_task_queue=queue;}    
    inline vector<gzip_task>* Get_out_task_queue()
    {
        vector<gzip_task>* ret=_out_task_queue;
        if(_out_task_queue!=NULL)
            _out_task_queue=NULL;
        return ret;
    }
private:
    pthread_t               _tid;
    vector<gzip_task>*      _waiting_task_queue;
    vector<gzip_task>*      _out_task_queue;
    vector<gzip_task>*      _working_task_queue;
    vector<gzip_task>       _complete_task_queue;
    GzipThdStatus           _status;
    GzipThdSignal           _signal;
};


class  compress_unit: private noncopyable
{
public:
        compress_unit(){}
        ~compress_unit(){}
        static  compress_unit* Instance();
        static  void    Destroy();
        int   Set_gzip_thread_num(int num);
        void  In_queue_task(gzip_task& task);
        void  Out_queue_task(vector<gzip_task>& task_queue);
        void  Process_waiting_task();
private:
        void Dispatch_task();
 
        int   _in_task_num;
        int   _out_task_num;
        vector<gzip_thread*>  _thread_vec;
        vector<gzip_task>     _waiting_task_queue;
        vector<gzip_task>     _complete_task_queue;
};


HTTP_SVR_NS_END

#endif
